// @charset "utf-8";

define(function(require, exports, module) {
	var publicMethod = require('./public'),
		clientApi = require('./clientapi');

	function StockModule() {
		this.code = '1A0001';
		this.tradeDay = '';
		this.init.apply(this, arguments);
	}

	StockModule.prototype.init = function (params) {
		var self = this;
		self.code = params.code;
		self.chartContent = params.content;

		self.requestData();
	}
	StockModule.prototype.requestData = function() {
		var self = this;
		var tradeDay = clientApi.getTradingDay();
		self.tradeDay = tradeDay;
		var quoteType = 'ZQMC,10,2525646,VOL,SETTLE,6';
		var getType = '10,2525646,VOL';

		var getData = function(param) {
			var rst = clientApi.thsQuote.getData2({
				type: quoteType,
				code: self.code,
				period: 'min',
				time: {
					begin: tradeDay,
					end: tradeDay,
					timeFmt: 1
				},
				updateMode: 1,
				timeStamp: 1
			});

			rstData = eval('(' + rst + ')');
			self.analyticalData(rstData, tradeDay);
		};
		clientApi.thsQuote.request({
			type: quoteType,
			code: self.code,
			period: 'min',
			begin: tradeDay,
			end: tradeDay,
			timeFmt: 1,
			onready: getData
		});
	}

	StockModule.prototype.analyticalData = function(data, tradeDay) {
		var self = this;
		var xjArr = [],
			zdfArr = [],
			zdfObj = {},
			volObj = {};
		var j = 0;
		$.each(data[self.code], function(i, val) {
			xjArr.push(parseFloat(val['10']));
			zdfArr.push(val['2525646']);
			zdfObj[publicMethod.xAxisData[j]] = parseFloat(val['2525646']).toFixed(2);
			volObj[publicMethod.xAxisData[j]] = parseFloat(val['VOL']).toFixed(2);
			j++;
		});
		self.drawChart(xjArr, zdfObj, volObj, zdfArr);
	}

	StockModule.prototype.drawChart = function(xjArr, zdfObj, volObj, zdfArr) {
		var self = this;

		var zdfCur = parseFloat(zdfArr[zdfArr.length - 1]).toFixed(2);
		var _color = '#fff';
		if (zdfCur > 0) {
			_color = 'red';
		} else if (zdfCur < 0) {
			_color = 'green';
		}

		var _tickPositions = [0, 120, 241],
			_tickInterval = '120',
			_xAxisMin = '0',
			_xAxisMax = '241',
			_xAxisData = publicMethod.xAxisData;
		var chartOptions = {
			credits: {
				enabled: false
			},
			title: {
				text: ''
			},
			chart: {
				backgroundColor: null,
				style: {
					"fontFamily": "Arial, \"Microsoft yahei\", Verdana, Helvetica, sans-serif",
					"fontSize": "12px"
				},
				marginTop: 10,
				marginLeft: 40,
				marginBottom: 30
			},
			series: [{
				name: '上证指数',
				type: 'area',
				// color: '#0186e4',
				// lineWidth: 1,
				// marker: {
				// 	enabled: false,
				// states: {
				// 	hover: {
				// 		fillColor: '#0186e4',
				// 		radius: 2,
				// 		lineColor: 'rgba(4, 103, 174, 1)',
				// 		lineWidthPlus: 1
				// 	}
				// }
				// },
				data: xjArr
			}],
			plotOptions: {
				series: {
					stickyTracking: false
				},
				area: {
					fillColor: {
						linearGradient: {
							x1: 0,
							y1: 0,
							x2: 0,
							y2: 1
						},
						stops: [
							[0, '#083a5f'],
							[1, Highcharts.Color('#0b1016').setOpacity(0).get('rgba')]
						]
					},
					marker: {
						radius: 2,
						states: {
							hover: {
								fillColor: '#0186e4',
								radius: 2,
								lineColor: '#fff',
								lineWidthPlus: 1
							}
						}
					},
					lineWidth: 1,
					lineColor: '#0186e4',
					states: {
						hover: {
							lineWidth: 1
						}
					},
					threshold: null
				}
			},
			xAxis: {
				lineWidth: 1,
				tickLength: 4,
				lineColor: '#808080',
				tickColor: '#808080',
				tickInterval: _tickInterval,
				tickPositions: _tickPositions,
				title: {
					text: ''
				},
				categories: _xAxisData,
				gridLineWidth: 0,
				labels: {
					align: 'center',
					rotation: 0,
					style: {
						color: '#afafaf'
					}
				},
				min: _xAxisMin,
				max: _xAxisMax
			},
			yAxis: {
				allowDecimals: false,
				lineWidth: 1,
				tickColor: '#808080',
				tickPosition: 'outside',
				gridLineWidth: 0,
				tickLength: 4,
				tickWidth: 1,
				title: {
					text: ''
				},
				lineColor: '#808080',
				labels: {
					style: {
						color: '#afafaf'
					},
					x: -7
				}
			},
			legend: {
				enabled: false,
				align: 'right',
				verticalAlign: 'top',
				floating: false,
				symbolHeight: 10,
				symbolWidth: 14,
				itemStyle: {
					color: '#dedede',
					fontFamily: 'Microsoft yahei',
					fontWeight: 'normal'
				},
				itemHiddenStyle: {},
				itemHoverStyle: {
					color: '#bbb'
				},
				useHTML: true
			},
			tooltip: {
				shared: true,
				backgroundColor: '#2e3339',
				borderColor: '#505458',
				shape: '',
				crosshairs: [{
					width: 1,
					color: '#afafaf'
				}],
				valueDecimals: 2,
				style: {
					fontFamily: 'Microsoft yahei',
					color: '#fff'
				},
				useHTML: true,
				formatter: function() {
					var color = '#fff';
					var zdfCur = zdfObj[this.x];
					if (zdfCur > 0) {
						color = 'red';
					} else if (zdfCur < 0) {
						color = 'green';
					}
					var head = self.tradeDay + ' ' + this.x + ' ' + '(北京时间)';
					var dqj = '当前价：&nbsp; <i class="' + color + '">' + (this.y).toFixed(2) + '</i>';
					var zdf = '涨跌幅：&nbsp; <i class="' + color + '">' + zdfObj[this.x] + '%</i>';
					var vol = '成交量：&nbsp; ' + (volObj[this.x] / 1000000).toFixed(2) + '万手';
					return head + '<br/>' + dqj + '<br/>' + zdf + '<br/>' + vol;
				}
			}
		};

		$(self.chartContent).highcharts(chartOptions);
	}

	module.exports = StockModule;
});